<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\book\controller\admin;

use app\book\base\BookAdminBase;

use app\book\model\BookCategory as BookCategoryModel;

class Category extends BookAdminBase
{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->submenu[] = ['title' => '栏目管理', 'url' => url('book/admin.category/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '添加栏目', 'url' => url('book/admin.category/add'), 'icon' => '&#xe654;'];
        $this->submenu[] = ['title' => '更新栏目缓存', 'url' => url('book/admin.category/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 栏目管理
     * @access public
     * @return \think\Response
     */
    public function index()
    {
        // 实例化模型
        $BookCategoryModel = new BookCategoryModel;
        // ajax操作
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'del') {
                $ids = $this->request->param('ids/a');
                if (empty($ids)) {
                    return mn_ajax_msg(0, '你还没有选择呢');
                }
                $BookCategoryModel->delCategory($ids);
                $BookCategoryModel->cache();
                return mn_ajax_msg(1, '删除成功');
            } elseif ($action == 'order') {
                $order = $this->request->param('data/a');
                if (empty($order)) {
                    return mn_ajax_msg(0, '你还没有选择呢');
                }
                $list = [];
                foreach ($order as $key => $value) {
                    if (isset($value['displayorder'])) {
                        $BookCategoryModel->where('id', (int)$key)->update(['displayorder' => (int)$value['displayorder']]);
                    }
                }
                $BookCategoryModel->cache();
                return mn_ajax_msg(1, '更新排序成功');
            } else {
                return mn_ajax_msg(0, '未知操作');
            }
        }
        // 修复栏目完整数据
        $BookCategoryModel->repair();
        // 模版变量赋值
        $this->assign([
            'submenu' => $this->submenu,
            'list' => $BookCategoryModel->getTreeTable(),
        ]);
        return $this->fetch();
    }

    /**
     * 栏目添加
     * @access public
     * @param int $pid 父栏目id
     * @return \think\Response
     */
    public function add($pid = 0)
    {
        $pid = (int)$pid;
        // 实例化栏目模型
        $book_category = new BookCategoryModel;
        // POST提交数据
        if ($this->request->isPost()) {
            //获取提交的数据
            $post_data = $this->request->param();
            //验证重复
            if ($book_category->where('dirname', $post_data['dirname'])->find()) {
                $this->error('栏目目录重复');
            }
            //保存到数据库
            if ($book_category->allowField(true)->validate(true)->save($post_data)) {
                $book_category->repair();
                $book_category->cache();
                $this->success('添加成功', 'index');
            } else {
                $this->error($book_category->getError());
            }
        }
        // 模版变量赋值
        $this->assign([
            'submenu' => $this->submenu,
            'pid' => $pid,
            'categorys' => $book_category->getTreeOption($pid)
        ]);
        return $this->fetch();
    }

    /**
     * 栏目修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0)
    {
        $id = (int)$id;
        if (!$id) {
            $this->error('缺少栏目id', 'index');
        }
        // 实例化栏目模型类
        $category = BookCategoryModel::get((int)$id);
        if (!$category) {
            $this->error('栏目不存在，请更新栏目缓存后重试', 'index');
        }
        // POST提交数据
        if ($this->request->isPost()) {
            //获取提交的数据
            $post_data = $this->request->post();

            // 父类不能是自己 或者 子类
            if ((int)$post_data['pid'] == $id || in_array((int)$post_data['pid'], explode(',', $category->childids))) {
                $this->error('父类不能是自己 或者 子类');
            }

            //验证重复
            $check_dirname = clone $category;
            if ($check_dirname->where('id', '<>', (int)$check_dirname->id)->where('dirname', $post_data['dirname'])->find()) {
                $this->error('栏目目录重复');
            }

            //更新到数据库
            if (false !== $category->allowField(true)->validate(true)->save($post_data, ['id' => (int)$category->id])) {
                $category->repair();
                $category->cache();
                $this->success('修改成功', 'index');
            } else {
                $this->error($category->getError());
            }
        }
        // 模版变量赋值
        $this->assign([
            'submenu' => $this->submenu,
            'data' => $category,
            'categorys' => $category->getTreeOption((int)$category->pid)
        ]);
        return $this->fetch();
    }

    /**
     * 栏目删除
     * @access public
     * @return void
     */
    public function del($id = 0, BookCategoryModel $book_category)
    {
        $id = (int)$id;
        if (!$id) {
            $this->error('缺少栏目id', 'index');
        }
        $book_category->delCategory($id);
        $book_category->cache();
        $this->success('删除成功', 'index');
    }

    /**
     * 更新缓存
     * @access public
     * @return void
     */
    public function cache(BookCategoryModel $book_category)
    {
        $book_category->cache();
        $this->success('更新缓存成功！', 'index');
    }
}